import 'package:syria_sports/presentation/admin_home_tabs/coaches_tab/widgets/coach_item_view.dart';

import '../../../../core/app_export.dart';
import '../../../../dialogs/soon_dialog.dart';
import '../../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import '../../../../widgets/app_bar/outline_icon_button.dart';
import '../../../../widgets/screen_state_widget.dart';
import '../../../coaches/add_coach_page/bloc/add_coach_page_screen.dart';
import 'coaches_tab_cubit.dart';
import 'package:flutter/material.dart';

class CoachesTabScreen extends StatelessWidget {
  const CoachesTabScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage(ImageConstant.imgBackground),
          fit: BoxFit.cover,
        ),
      ),
      child: Scaffold(
        appBar: _buildAppBar(context: context),
        body: BlocConsumer<CoachesTabCubit, CoachesTabState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            var cubit = CoachesTabCubit.get(context);

            print(height/width );
            print(width/height );

            return Container(
              height: height,
              width: width,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage(ImageConstant.imgBackground),
                  fit: BoxFit.cover,
                ),
              ),
              child: ScreenStateWidget(
                isDone: state is CoachesTabDone,
                isError: state is CoachesTabError,
                isLoading: state is CoachesTabLoading,
                onRefresh: cubit.getAllUsers,
                onDone: GridView.builder(
                    gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
                      maxCrossAxisExtent: 410,
                      mainAxisExtent: 150,
                      //childAspectRatio:(height>width)? height/width:width/height,
                    ),
                    itemCount: cubit.users.length,
                    itemBuilder: (context,index){
                      return CoachItemView(user: cubit.users[index]);
                    }),
              ),

            );
          },
        ),
      ),
    );
  }
  PreferredSizeWidget _buildAppBar({required BuildContext context}) {
    return CustomAppBar(
      actions: [
        if (PrefUtils.isGymAdmin())
          OutlineIconButton(
          icon: Icons.add,
          margin:const EdgeInsets.all(8),
          padding:const EdgeInsets.all(2),
          onTapped: ()async{
            showPlayerDialog(context);
          },
        ),
      ],
      title: AppbarSubtitle(text: 'المدربين'),
    );
  }
  void showPlayerDialog(BuildContext context1) {
    showDialog(
      context: context1,
      builder: (BuildContext context) {
        return AlertDialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(15.0),
          ),
          title: const Text('إضافة مدرب'),
          content: const Text('اختر طريقة الإضافة:'),
          actions: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                ElevatedButton(
                  onPressed: () async {
                    Navigator.of(context).pop();
                    await Navigator.push(context, MaterialPageRoute(builder: (context) => const AddCoachPageScreen())).then((value) {
                      CoachesTabCubit.get(context).getAllUsers();
                    }).then((value) {});
                  },
                  style: ElevatedButton.styleFrom(
                    padding: const EdgeInsets.all(16),
                    backgroundColor: Colors.orangeAccent,
                  ),
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Text('إضافة مدرب جديد',
                        style: theme.textTheme.titleMedium),
                  ),
                ),
                const SizedBox(height: 10),
                ElevatedButton(
                  onPressed: () {
                    Navigator.of(context).pop();
                    // Action for adding player from platform
                    showDialog(
                        barrierDismissible: true,
                        context: NavigatorService.navigatorKey.currentState!.overlay!.context,
                        builder: (BuildContext context) {
                          return const SoonDialog();
                        });
                  },
                  style: ElevatedButton.styleFrom(
                    padding: const EdgeInsets.all(16),
                    backgroundColor: Colors.deepOrange,
                  ),
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Text(
                      'إضافة مدرب من المنصة',
                      style: theme.textTheme.titleMedium,
                    ),
                  ),
                ),
              ],
            ),
          ],
        );
      },
    );
  }
}
